package com.ostms.adv.utils;

import android.util.Log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 自定义记录日志类
 */
public class LOG {
    /**
     * 日志文件后缀
     */
    public static final String SUFFIX = ".log";

    private static String type = "adv";
    public static LOG log;

    /**
     * 适用打印异常日志
     *
     * @param tag       类名.方法名
     * @param throwable 异常类
     */
    public static void e(String tag, Throwable throwable) {

        try {
            String message = messageHead(tag) + throwable.getClass().getSimpleName()
                    + "：" + throwable.getMessage();
            //logcat打印
            System.out.println(message);
            //将消息加入普通日志文件
            appendMessageToFile(getLogFile(false), message, true);
            //将消息将入异常日志文件
            StringWriter sw = new StringWriter();
            throwable.printStackTrace(new PrintWriter(sw, true));
            System.out.println("异常打印：" + sw.toString());
            appendMessageToFile(getLogFile(true), sw.toString(), true);
        } catch (IOException e) {
            System.out.println("日志打印异常" + e.getClass() + ":" + e.getMessage());
        }
    }

    /**
     * 适用打印异常日志
     *
     * @param tag     类名.方法名
     * @param message 异常消息
     */
    public static void e(String tag, String message) {

        try {
            message = messageHead(tag) + message;
            //logcat打印
            System.out.println(message);
            //将消息加入普通日志文件
            appendMessageToFile(getLogFile(false), message, true);
            //将消息将入异常日志文件
            appendMessageToFile(getLogFile(true), message, true);
        } catch (IOException e) {
            System.out.println("日志打印异常" + e.getClass() + ":" + e.getMessage());
        }
    }

    /**
     * 打印普通日志
     *
     * @param tag
     * @param message
     */
    public static void i(String tag, String message) {

        try {
            message = messageHead(tag) + message;
            //logcat打印
            System.out.println(message);
            //将消息加入普通日志文件
            appendMessageToFile(getLogFile(false), message, false);
        } catch (IOException e) {
            System.out.println("日志打印异常" + e.getClass() + ":" + e.getMessage());
        }
    }

    /**
     * 拼接消息头：线程ID,日期时间，tag
     *
     * @param tag
     * @return
     */
    public static String messageHead(String tag) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return String.format("%5d", Thread.currentThread().getId()) + ") "
                + format.format(new Date()) + " [" + tag + "]：";
    }

    /**
     * 得到日志文件
     *
     * @param isException
     * @return
     */
    private static File getLogFile(boolean isException) throws IOException {
        //日志文件夹
        String path = CommonUtils.BASE_FILEPAHT + CommonUtils.LOG_PATH;
        File directory = new File(path);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        //日志文件
        String fileName = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        if (isException) {
            fileName = format.format(new Date()) + "_" + type + "Exception" + SUFFIX;
        } else {
            fileName = format.format(new Date()) + "_" + type + SUFFIX;
        }
        File logFile = new File(directory, fileName);
        if (!logFile.exists()) {
            logFile.createNewFile();
        }
        return logFile;
    }

    /**
     * 在文件末尾添加日志信息
     *
     * @param file
     * @param message
     */
    private static void appendMessageToFile(File file, String message, boolean isException) {
        try {
            FileWriter fw = new FileWriter(file, true);
            if (isException) {
                fw.append("==============运行异常/Exception==============" + "\r\n");
            }
            fw.append(message + "\r\n");
            fw.flush();
        } catch (IOException e) {
            Log.e("LOG.appendMessageToFile", "异常", e);
        }
    }
}
